//
//  HomeHeaderView.swift
//  HengYouCai
//
//  Created by lwj on 2017/11/9.
//  Copyright © 2017年 lwj. All rights reserved.
//

import UIKit

class MenuModel: BaseObject {
    var imageName = ""
    var title = ""
    var linkUrl = ""
    var action = ""
    
}


class HomeCollectionCell: UICollectionViewCell {
    //
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.white
        initUI()
    }
    func initUI(){
        self.addSubview(menuImg)
        menuImg.snp.makeConstraints { (make) in
            make.top.equalTo(8)
            make.centerX.equalToSuperview()
            make.height.equalTo(35)
            make.width.equalTo(35)
        }
        self.addSubview(menuTitle)
        menuTitle.snp.makeConstraints { (make) in
            make.top.equalTo(menuImg.snp.bottom).offset(5)
            make.centerX.equalToSuperview()
        }
    }
    func setModel(model:MenuModel) {
        let url = URL(string: model.imageName)
        menuImg.kf_setImage(with: ImageResource(downloadURL: url!))
        menuTitle.text = model.title
    }
    lazy var menuImg: UIImageView = {
        let temp = UIImageView()
        temp.image = UIImage(named: "insurance")
        temp.contentMode = .scaleAspectFit
        return temp
    }()
    lazy var menuTitle: UILabel = {
        let temp = UILabel()
        temp.font = Font_WL(11)
        temp.textColor = Normal_333Color
        temp.textAlignment = .center
        temp.text = ""
        return temp
    }()
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}



protocol MenuDelegate : NSObjectProtocol {
    // 点击menu按钮的代理协议
    func menuTapDelegate(menuModel : MenuModel)
}
class HomeHeaderView: UIView,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
//    var a[me]
    weak var menuDelegate : MenuDelegate?
    /// 四个菜单键的数据源
    var menuModels :Array<MenuModel> = Array()
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return menuModels.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let collection = collectionView.dequeueReusableCell(withReuseIdentifier: "HomeCollectionCellID", for: indexPath) as! HomeCollectionCell
        collection.setModel(model: menuModels[indexPath.row])
        return collection
        
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize{
        
        let cellNum : CGFloat = CGFloat(menuModels.count)
        
        return CGSize(width: (JCWSCREENW - 30 )/cellNum, height: 77)
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let model = menuModels[indexPath.item]
        menuDelegate?.menuTapDelegate(menuModel : model )
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = Normal_458Color
        self.initUI()
    }
    
    func initUI() {
        
//        self.addSubview(self.bannerView)
//        _ = self.bannerView.sd_layout()
//            .topSpaceToView(self,0)?
//            .leftSpaceToView(self,0)?
//            .widthIs(JCWSCREENW)?
//            .heightIs((145.0/375)*375)
//        self.addSubview(noticeView)
//        _ = noticeView.sd_layout()
//            .topSpaceToView(bannerView,0)?
//            .rightSpaceToView(self,0)?
//            .heightIs(30)?
//            .widthIs(JCWSCREENW)
//
//
//        self.addSubview(menuCollectionView)
//
//        self.addSubview(redPacket)
//        redPacket.snp.makeConstraints { (make) in
//            make.top.equalTo(menuCollectionView.snp.bottom).offset(7)
//            make.left.equalToSuperview()
//            make.right.equalToSuperview()
//            make.height.equalTo(78)
//        }
        self.addSubview(self.bannerView)
        _ = self.bannerView.sd_layout()
            .topSpaceToView(self,0)?
            .leftSpaceToView(self,0)?
            .widthIs(JCWSCREENW)?
            .heightIs(215)
        
        self.addSubview(noticeView)
        _ = noticeView.sd_layout()
            .topSpaceToView(bannerView,42)?
            .rightSpaceToView(self,0)?
            .heightIs(35)?
            .widthIs(JCWSCREENW)
        
        self.addSubview(menuCollectionView)
        
        self.addSubview(redPacket)
        _ = redPacket.sd_layout()
            .topSpaceToView(noticeView,0)?
            .leftSpaceToView(self,0)?
            .heightIs(78)?
            .widthIs(JCWSCREENW)
      
        
    }
    
    func setMenuView(_ models:Array<MenuModel>){
        self.menuModels = models
        self.menuCollectionView.reloadData()
    }
    /// banner图
    lazy var bannerView: SDCycleScrollView = {
        let banner = SDCycleScrollView()
        banner.autoScrollTimeInterval = 5
        banner.autoScroll = true
//        banner.placeholderImage = UIImage(named: "home_banner")
        banner.imageURLStringsGroup = [""]
        banner.pageControlStyle = SDCycleScrollViewPageContolStyleClassic
        banner.pageControlDotSize = CGSize(width: 6, height: 6)
        return banner
    }()
    
    /// 消息view
    lazy var noticeView: UIView = {
        let noticeView = UIView(frame: CGRect(x: 0, y: 0, width: JCWSCREENW, height: 35))
        noticeView.backgroundColor = UIColor.white
        /// 小喇叭
        
        let noticeImg = UIImageView(image: UIImage(named : "notice"))
        noticeView.addSubview(noticeImg)
        _ = noticeImg.sd_layout()
            .centerYEqualToView(noticeView)?
            .leftSpaceToView(noticeView,15)?
            .heightIs(10)?
            .widthIs(25)
        
//        /// 竖直的线条
        let lineView = UIView()
        lineView.backgroundColor = Normal_CCCColor
        noticeView.addSubview(lineView)
        _ = lineView.sd_layout()
            .leftSpaceToView(noticeImg,7)?
            .centerYEqualToView(noticeView)?
            .heightIs(10)?
            .widthIs(0.5)
        
        noticeView.addSubview(self.articleView)
        _ = self.articleView.sd_layout()
            .topSpaceToView(noticeView,0)?
            .leftSpaceToView(noticeImg,10)?
            .rightSpaceToView(noticeView,10)?
            .bottomSpaceToView(noticeView,0)
        return noticeView
    }()
    
    lazy var articleView : WCarouselView = {
        
        let temp = WCarouselView()
        temp.scrollingDirection = WScrollingDirectionVertical
        temp.backgroundColor = UIColor.white
        temp.isContinuous = true
        temp.autoPlayTime = 3.0
        
        return temp
    }()
    /// 新人见面礼
    lazy var redPacket: UIButton = {
        let redBtn = UIButton(type: UIButtonType.custom)
        redBtn.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 10)
//        redBtn.setBackgroundImage(UIImage(named: "new"), for: .normal)
//        redBtn.setBackgroundImage(UIImage(named: "new"), for: .highlighted)
        return redBtn
    }()
    lazy var menuCollectionView: UICollectionView = {
        
        var layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = 0
        layout.scrollDirection = .horizontal
        
        let temp = UICollectionView(frame: CGRect(x: 15, y: self.bannerView.height-33, width: JCWSCREENW - 30, height: 73), collectionViewLayout: layout)
        temp.backgroundColor = UIColor.white
        temp.delegate = self
        temp.dataSource = self
        temp.register(HomeCollectionCell.self, forCellWithReuseIdentifier: "HomeCollectionCellID")
        return temp
    }()
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
}
